package com.livestocksolutions;

import com.livestocksolutions.util.UpdateHistory;

/* -- Version History --
 *  1.0 : 25 July 2012 - Initial version.
 *
 *  1.1 : 25 July 2012 - Changed currentJoinings from Join[] to ArrayList<Join>
 *  
 *  1.2 : 31 July 2012 - Added getLocation() to get the location
 *  
 *  1.3 : 06 September 2012 - Added serialVersionUID to reflect super class being serialisable
 *                        Changed private Paddock location to private long location,
 *                        Changed private ArrayList<Join> currentJoinings = new ArrayList<Join>() to
 *                         private ArrayList<Long> currentJoinings = new ArrayList<Long>().
 *                        Updated public void setLocation(Paddock), public Paddock getLocation(),
 *                         public Join[] getCurrentJoinings() and public void applyJoiningToMob(Join joining)
 *                         to reflect the changing of the types for location and currentJoinings.
 *  1.4 : 07 October 2012 - Moved Joining apply methods and associated data to Group class so that a Joining can also
 *                        be made to a group of Cattle too.
 */

/**
 * This class creates a Mob grouping.
 * @author Adam Parr
 * @version 1.4
 * 
 * @.modified 07 October 2012
 */
public class Mob extends Group {
	/**
	 * The Current physical location of the mob
	 */
	private long location;
	
	/**
	 * Constructor to create a new Mob
	 * @param string the name of this Mob
	 */
	public Mob(String string){
		super(string);
	}
	
	/**
	 * Set the new location of this Mob to the given paddock
	 * @param location the paddock where the mob has been moved to
	 */
	public void setLocation(Paddock location){
		if(location==null||this.location!=location.getID()){
			if(location!=null){
				this.location=location.getID();
			}else{
				this.location=0;
			}
			update.put("LOCATION", new UpdateHistory(String.valueOf(this.location)));
		}
	}
	
	/**
	 * Get the location of this mob
	 * @return A Paddock where this mob is
	 */
	public Paddock getLocation(){
		return (Paddock)LivestockSolutions.getLocationByID(location);
	}
	
	/**
	 * Updates this Object with the updated values from the different version.
	 * @param changes Mob that represents this Mob in a different version.
	 * @return <tt>true</tt> if changes is not null and changes does not equal this object, <tt>false</tt> otherwise.
	 */
	public boolean update(Mob changes){
		if(changes!=null && this!=changes){
			super.update(changes);
			
			if(updateBefore("LOCATION",changes)){
				location=changes.location;
				update.put("LOCATION", new UpdateHistory(String.valueOf(location)));
			}
			return true;
		}
		return false;
	}
}
